﻿using StarPizza.ViewModel.Core;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StarPizza.ViewModel.ClientSystem
{
    class Cart : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        public PizzaVM SelectedPizzaVM { get; set; }
        public ObservableCollection<PizzaVM> OrderedPizzas {get; set;}
        OrderController orderController;

        public int SumPrice
        {
            get { return OrderedPizzas.Sum(pizzaVM => pizzaVM.Price); }
        }

        public Cart(OrderController orderController)
        {
            this.orderController = orderController;
            OrderedPizzas = new ObservableCollection<PizzaVM>();
        }
        public void AddPizza(PizzaVM pizza)
        {
            OrderedPizzas.Add(pizza);
            OnPropertyChanged("SumPrice");
        }
        void CancelPizza(PizzaVM pizza)
        {
            OrderedPizzas.Remove(pizza);
            orderController.CancelPizza(pizza);
            OnPropertyChanged("SumPrice");
        }
        public void FinalizeOrder()
        {
            orderController.FinalizaOrder(OrderedPizzas);
            OrderedPizzas.Clear();
            OnPropertyChanged("SumPrice");
        }
        public void CancelSelectedPizza()
        {
            CancelPizza(SelectedPizzaVM);
        }
    }
}
